package com.banshuibao.bsb_android.util;

import java.security.MessageDigest;

/**
 * Created by Administrator on 2016/11/12.
 */

public class MD5 {

    /**
     * MD5加密
     *
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] encryptMD5(byte[] data) throws Exception {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(data);
        return md5.digest();
    }


    public static String encryptMD5ToString(byte[] data) throws Exception {
        byte[] bytes = encryptMD5(data);
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < bytes.length; i++){
            int val = ((int) bytes[i]) & 0xff;
            if (val < 16)
                sb.append("0");
            sb.append(Integer.toHexString(val));
        }

        return sb.toString();
    }


}
